<?php
# vim:ts=2:sw=2:noexpandtab
#
require_once('oauth_oss.inc.php');
require_once('narcis_dai.inc.php');

$osdata = get_opensocial_data();

// generic checks
if (!array_key_exists('viewer',$osdata))  { throw new Exception("No viewer in OpenSocial data");  }
if (!array_key_exists('appData',$osdata)) { throw new Exception("No appdata in OpenSocial data"); }
$person  = $osdata['viewer'];
$appData = $osdata['appData'];
if (!$person || !$person['id'] || !$person['name']) { throw new Exception("No viewer data found"); }


$dai = NULL;
if (array_key_exists('MRP_DAI',$appData))
{
	$dai = $appData['MRP_DAI']['dai'];
	$daiPerson = narcis_search_dai($dai);
}
$daiReqDate = "0";
if (array_key_exists('getdai_gadget',$appData))
{
	$daiReqDate = $appData['getdai_gadget']['daiReqDate'];
}

# fetch persons with the same name as user from Narcis
$initial = substr( $person['name']['givenName'], 0, 1 );
$persons = narcis_search_person($person['name']['familyName'],$initial);
#### DEBUG
#### $persons = narcis_search_person('Beenakker','C');

$persons_html = '';
foreach ($persons as $p)
{
	$entry  = '';
	$entry .= '<div class="p_entry" onclick="personClicked(this)">';
	$entry .= '<div class="p_name">';
	$entry .= '<a href="' . $p['link'] . '" target="_blank">' . $p['name'] . '</a>';
	$entry .= '</div>';
	$entry .= '<div class="p_org">';
	$entry .= $p['field'];
	$entry .= '</div>';
	$entry .= '<div class="p_desc">';
	$entry .= $p['exp'] ? $p['exp'].';' : '';
 	$entry .= $p['cat'];
	$entry .= '</div>';
	$entry .= '<div class="p_dai">';
	$entry .= $p['dai'] ? "DAI: {$p['dai']}" : 'No DAI assigned';
	$entry .= '</div>';
	$entry .= '</div>';

	$persons_html .= $entry;
}

print '<style type="text/css">';
# logic to determine what to show
if ($dai)
{
	print '#daiCurrent   { display: block; }';
	print '#daiRequested { display: none;  }';
	print '#daiEntry     { display: none;  }';
	print '#daiFind      { display: none;  }';
}
elseif ($daiReqDate)
{
	print '#daiCurrent   { display: none;  }';
	print '#daiRequested { display: block; }';
	print '#daiEntry     { display: block; }';
	print '#daiFind      { display: none;  }';
}
else
{
	print '#daiCurrent   { display: none;  }';
	print '#daiRequested { display: none;  }';
	print '#daiEntry     { display: block; }';
	print '#daiFind      { display: block; }';
}
print '</style>';

?>

<link rel="stylesheet" type="text/css" href="getdai.css" />

<div id="daiCurrent">
	<div id="daiFound">
		Based on your DAI, we found the following profile information to be used in this portal.
		If this is not your profile, please <a href="#" onClick="return resetDAI()">reset the
		DAI</a> to be used here.
	</div>
	<div id="theDai">
		Your DAI: <?= $dai ?>
	</div>
	<div id="daiProfile">
		<div class="p_name"><a href="<?php print $daiPerson['link'] ?>" 
		   target="_blank"><?= $daiPerson['name'] ?></a><br/>
			<span class="p_org" ><?= $daiPerson['field'] ?><br/></span>
			<span class="p_desc"><?= $daiPerson['exp'] ?></span>
		</div>
	</div>
</div>

<div id="daiRequested">
	Your request has been sent to the DAI authority on <?= $daiReqDate ?>.  
	Soon you will receive an email containing your DAI.  Fill it in below to continue.<br/>
	Or <a href="#" onclick="return resetDAIRequest()">return</a> to meanwhile look 
	it up yourself.
</div>

<div id="docs">
	A Digital Author ID (DAI) is a unique name by 
	which authors of scientific articles in the Netherlands are identified.
	See <a target="_blank" href="http://www.surffoundation.nl/en/themas/openonderzoek/infrastructuur/Pages/digitalauthoridentifierdai.aspx">this site</a> for more information.
	<br>
	Use this gadget to find and set your DAI.  There are several possibilities:
  <ul>
    <li>If your DAI is already known (see above), you don't have to do anything;
		<li>If your DAI is listed in one of the library records below, or if you
		know it from another source, please enter it in the form below and click on the
		"Set DAI" button;
		<li>If you are not in the list, and you don't know your DAI, press the "Request a 
		new DAI" button.  A mail will be sent to your local librarian requesting a DAI.
	</ul>
</div>


<div id="daiEntry">
	<form name="daiForm" action="#">
		Enter your DAI: 
		<sup><a target="_blank" href="http://www.surffoundation.nl/en/themas/openonderzoek/infrastructuur/Pages/digitalauthoridentifierdai.aspx">What's this?</a></sup>
		<input type="text" name="dai" id="daiInput" size="30" value="<?= $dai ? $dai : '' ?>"/>
		<input type="submit" value="Set"   onclick="return setDAI()"  />
	</form>
</div>

<div id="daiFind">
	<div id="daiSearch">
		Don't know your DAI?  Look it up in your profile below or 
		<a href="#" onclick="return showRequestDAI()">request a DAI</a>.
		<div id="daiRequest">
			<div id="requestAck">
				<b>Request a new DAI:</b><br/>
				The following information will be sent to your local DAI authority: 
				<ul>
					<li>Name:  <?= $person['displayName']        ?></li>
					<li>Email: <?= $person['emails'][0]['value'] ?></li>
				</ul>
				<input type="submit" id="requestDaiButton" 
				  value="Submit" onclick="return requestDAI()" />
			</div>
	</div>
		<div id="narcis_persons">
			<?= $persons_html ?>
		</div>
		<div id="personSource">(source: persons from <a target="_blank" 
		 href="http://www.narcis.nl/">NARCIS</a>
		 matching &quot;<?= $person['name']['familyName'] ?>&quot;)</div>
	</div>


</div>

<script type="text/javascript" src="getdai.js"></script>
<script type="text/javascript">
	var daiRequestDate = '<?= $daiReqDate ?>';
  var osData = '<?= osdata2json($osdata) ?>';
	gadgets.util.registerOnLoadHandler( onLoad(daiRequestDate,osData) );
</script>

